home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / createSkinCluster.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  8.7 KB  |  345 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. // Copyright (C) 1997-1998 Alias|Wavefront,
  18. // a division of Silicon Graphics Limited.
  19. //
  20. // The information in this file is provided for the exclusive use of the
  21. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  22. // and incorporate this code into other products for purposes authorized
  23. // by the Alias|Wavefront license agreement, without fee.
  24. //
  25. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  26. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  27. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  28. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  29. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  30. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  31. // PERFORMANCE OF THIS SOFTWARE.
  32. //
  33. //
  34. //    Alias|Wavefront Script File
  35. //    MODIFY THIS AT YOUR OWN RISK
  36. //
  37. //    Creation Date:  3 Aug 1998
  38. //    Author:         Vangelis Kokkevis
  39. //
  40. //  Procedure Name:
  41. //      createSkinCluster
  42. //
  43. //  Description:
  44. //      Creates a skinCluster out of each of the selected 
  45. //      pieces of geometry
  46. //
  47. //      Example:
  48. //         Select all the geometries to be included in the skin
  49. //         and one joint and run "createSkinCluster".  
  50. //
  51. //         If multiple joints are selected then the skinCluster 
  52. //         will be bound only to the individual joints
  53. //
  54.  
  55. proc string connectedToSkinCluster(string $node)
  56. //
  57. // Description:
  58. //    Returns the name of the skinCluster in the history of
  59. //    a particular node.  If there's no skinCluster in the 
  60. //    history then it returns the empty string
  61. //
  62. {
  63.     string $histL[] = `listHistory $node`;
  64.     string $hist;
  65.     for($hist in $histL)
  66.         if (nodeType($hist) == "skinCluster")
  67.             return $hist;
  68.     
  69.     return "";
  70. }
  71.  
  72.  
  73. global proc string[] createSkinCluster(string $args)
  74. {
  75.     string $result[];
  76.     int $clusterCount;
  77.  
  78.     // Get the currenly selected objects
  79.     //
  80.     string $sel[] = `ls -sl`;
  81.     if (!size($sel)) {
  82.         error("Select objects to bind.");
  83.         return $result;
  84.     }
  85.  
  86.     // Find the joints in the selection list
  87.     //
  88.     int $i;
  89.     string $joints;
  90.     int $jointCount;
  91.     for($i=0;$i<size($sel);$i++)
  92.     {
  93.         $item = $sel[$i];
  94.         if (nodeType($item) == "joint")
  95.         {
  96.             $joints = $joints + " " + $item;
  97.             $jointCount++;
  98.         }
  99.     }
  100.  
  101.     if ($jointCount == 0)
  102.     {
  103.         error("You must select at least one joint.");
  104.         return $result;
  105.     }
  106.  
  107.     // Create the skin clusters
  108.     //
  109.     string $cmd;
  110.     int $shapeCount = 0;
  111.     int $j;
  112.     string $validShapes[];
  113.     string $componentShapes[];
  114.     int $compShapeCount = 0;
  115.     string $buf[];
  116.     for($i=0;$i<size($sel);$i++)
  117.     {
  118.         $item = $sel[$i];
  119.         if (nodeType($item) != "joint")
  120.         {
  121.             // Check if we are dealing with a whole shape 
  122.             // or just individual components
  123.             //
  124.             if (tokenize($item,".",$buf) == 1)
  125.             {
  126.                 string $allShapesCmd = "ls -lf -dag "+"\""+$item+"\"";
  127.                 string $allShapes[] = eval($allShapesCmd);
  128.                 for ($j=0;$j<size($allShapes);$j++)
  129.                 {
  130.                     string $nt[] = `ls -type controlPoint $allShapes[$j]`;
  131.                     int $io = `getAttr ($allShapes[$j]+".io")`;
  132.                     if (size($nt) && ! $io) {
  133.                         int $found = 0;
  134.                         for($vShape in $validShapes) {
  135.                             if ($vShape == $allShapes[$j]) {
  136.                                 $found = true;
  137.                                 break;
  138.                             }
  139.                         }
  140.                         if (!$found) {
  141.                             $validShapes[$shapeCount++] = $allShapes[$j];
  142.                         }
  143.                     }
  144.                 }
  145.             }
  146.             else
  147.             {
  148.                 string $nt[] = `ls -lf -dag -type controlPoint $buf[0]`;
  149.                 int $io = `getAttr ($buf[0]+".io")`;
  150.                 if (size($nt) && !$io)
  151.                 {
  152.                     // Check if other components of that shape 
  153.                     // are in the same list
  154.                     //
  155.                     int $found = false;
  156.                     string $cShape;
  157.                     for($cShape in $componentShapes)
  158.                         if ($cShape == $buf[0])
  159.                         {
  160.                             $found = true;
  161.                             break;
  162.                         }
  163.                     if (!$found)
  164.                     {
  165.                         $componentShapes[$compShapeCount++] = $buf[0];
  166.                     }
  167.                 }
  168.             }
  169.         }
  170.     }
  171.  
  172.     // Deal with surfaces bound as a whole
  173.     //
  174.     string $cmd;
  175.     for ($j = 0; $j < $shapeCount; $j++) {
  176.         $cmd = "skinCluster "+ $args + " ";
  177.         $cmd = $cmd + $joints + " " + $validShapes[$j];
  178.         string $res[];
  179.  
  180.         if ($j == ($shapeCount-1) && 0 == $clusterCount) {
  181.             // on the very last shape, if we have not yet created any
  182.             // clusters, we eval without a catch so that the script will
  183.             // quit on the error rather than printing out an empty result
  184.             //
  185.             $res = eval($cmd);
  186.             $result[$clusterCount++] = $res[0];
  187.         } else {
  188.             if (!catch($res = eval($cmd))) {
  189.                 $result[$clusterCount++] = $res[0];
  190.             }
  191.         }
  192.     }
  193.  
  194.     // Deal with surfaces whose components are bound
  195.     //
  196.     string $cShape;
  197.     int $count = 0;
  198.     for ($cShape in $componentShapes)
  199.     {
  200.         // If the surface already has a skin cluster attached
  201.         // then just add the selected points to the existing 
  202.         // skin cluster set
  203.         //
  204.         string $skinCluster = connectedToSkinCluster($cShape);
  205.         int $foundSet = false;
  206.         if ($skinCluster != "")
  207.         {
  208.             if ("toSelectedBones" == match("toSelectedBones",$args)) {
  209.                 // Are the selected bones influence objects yet? If not
  210.                 // we need to add them as influence objects. We'll also lock
  211.                 // all weights while we do this so we do not affect anything
  212.                 // else in the scene.
  213.                 //
  214.  
  215.                 // First, find the missing influences
  216.                 //
  217.                 string $currentInfluences[] = `skinCluster -q -inf $skinCluster`;
  218.                 string $selectedJoints[] = `ls -sl -type transform`;
  219.                 string $missingInfluences[];
  220.                 int $sizeMI = 0;
  221.                 for ($sJoint in $selectedJoints) {
  222.                     int $found = 0;
  223.                     for ($cInfluence in $currentInfluences) {
  224.                         if ($sJoint == $cInfluence) {
  225.                             $found = 1;
  226.                             break;
  227.                         }
  228.                     }
  229.                     if (! $found) {
  230.                         $missingInfluences[$sizeMI] = $sJoint;
  231.                         $sizeMI += 1;
  232.                     }
  233.                 }
  234.  
  235.                 // Now, add the missing influences
  236.                 //
  237.                 if ($sizeMI > 0) {
  238.                     // lock all other influences
  239.                     //
  240.                     string $currentlyLockedInfluences[];
  241.                     int $sizeCLI = 0;
  242.                     for ($cInfluence in $currentInfluences) {
  243.                         if (getAttr ($cInfluence+".liw")) {
  244.                             $currentlyLockedInfluences[$sizeCLI] = $cInfluence;
  245.                             $sizeCLI += 1;
  246.                         }
  247.                         setAttr ($cInfluence+".liw") 1;
  248.                     }
  249.  
  250.                     // actually add the missing influences
  251.                     //
  252.                     for ($mInfluence in $missingInfluences) {
  253.                         skinCluster -e -ai $mInfluence $skinCluster;
  254.                     }
  255.  
  256.                     // unlock the other influences, unless they were previously
  257.                     // locked
  258.                     //
  259.                     for ($cInfluence in $currentInfluences) {
  260.                         int $found = 0;
  261.                         for ($lInfluence in $currentlyLockedInfluences) {
  262.                             if ($lInfluence == $cInfluence) {
  263.                                 $found = 1;
  264.                                 break;
  265.                             }
  266.                         }
  267.                         if (! $found) {
  268.                             setAttr ($cInfluence+".liw") 0;
  269.                         }
  270.                     }
  271.                 }
  272.             }
  273.             
  274.             string $connL[] = `listConnections -type objectSet ($skinCluster+".message")`;
  275.             string $conn;
  276.             for ($conn in $connL)
  277.             {
  278.                 if (nodeType($conn) == "objectSet")
  279.                 {
  280.                     $foundSet = true;
  281.                     $cmd = "sets -fe " + $conn + " ";
  282.                 }
  283.             }
  284.         }
  285.  
  286.         // If a skinCluster is not found then just use
  287.         // the skinCluster command to create a new one
  288.         //
  289.         if (!$foundSet)
  290.             $cmd = "skinCluster "+ $args + " " + $joints + " ";
  291.  
  292.         for($s in $sel)
  293.         {
  294.             if (tokenize($s, ".", $buf) != 1 &&
  295.                 $buf[0] == $cShape)
  296.             {
  297.                 
  298.                 $cmd = $cmd + $s + " ";
  299.             }
  300.         }
  301.  
  302.         string $res[];
  303.         if ($count == ($compShapeCount-1) && 0 == $clusterCount) 
  304.         {
  305.             if ($foundSet)
  306.             {
  307.                 eval($cmd);
  308.                 $result[$clusterCount++] = $skinCluster;
  309.             }
  310.             else
  311.             {
  312.                 $res = eval($cmd);
  313.                 $result[$clusterCount++] = $res[0];
  314.             }
  315.         } 
  316.         else 
  317.         {
  318.             if ($foundSet)
  319.             {
  320.                 if (!catch(eval($cmd)))
  321.                     $result[$clusterCount++] = $skinCluster;
  322.             }
  323.             else
  324.             {
  325.                 if (!catch($res = eval($cmd))) 
  326.                     $result[$clusterCount++] = $res[0];
  327.             }
  328.         }
  329.      
  330.         $count++;
  331.     }
  332.         
  333.     
  334.     if ($shapeCount == 0 && $compShapeCount == 0) {
  335.         error("Bind-able objects must be selected.");
  336.     }
  337.  
  338.     return $result;
  339. }
  340.  
  341.  
  342.                 
  343.  
  344.  
  345.